package AlexDB.Client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientApp {
    public static void main(String[] args) {
        try {

            BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("connect:");
            var connectAddress = bReader.readLine().split(" ");
            Socket socket = new Socket(connectAddress[0],Integer.parseInt(connectAddress[1]));
            boolean isRun = true;
            while (isRun) {
                var command = bReader.readLine();
                ObjectOutputStream objOutputStream = new ObjectOutputStream(socket.getOutputStream());
                objOutputStream.writeUTF(command);
                objOutputStream.flush();

                if (command.equals("exit")) {
                    socket.close();
                    isRun = false;
                    break;
                }

                try {
                    ObjectInputStream dataInputStream = new ObjectInputStream(socket.getInputStream());
                    var obj = dataInputStream.readObject();
                    System.out.println("[server]: " + obj.toString());
                } catch (Exception e) {
                    System.out.println("[server]: error or null.");
                }

            }
            socket.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

}
